<?php

namespace app\common\model;

use think\Model;

class VipOrder extends Model
{
    // 表名
    protected $name = 'vip_order';



    /**
     * 支付回调
     */
    public function notify_order($orderNo, ?string $pay_sn = '', ?string $payMoney = '0')
    {
        $info = self::where('order_sn', $orderNo)->find();
        if (!$info) fail("订单不存在");
        if ($info['status'] == '1') fail("订单已支付");
        $info->status = '1';
        $info->pay_time = time();
        # 微信流水号
        if ($pay_sn) {
            $info->pay_sn = $pay_sn;
            $info->pay_money = $payMoney;
        }
        # 更新vip 到期时间
        $user = (new User)->where('id', $info->user_id)->find();
        if (!empty($user)) {
            $time = $user->vipend_time > time() ? $user->vipend_time + $info->day * 86400 : time() +  $info->day * 86400;
            $user->vipend_time = $time;
            $user->save();
        }
        # 更新简批资源
        (new UserExplaining())->getSimpleAdd($info->user_id, $info->simplified_batch);
        $info->save();
        return true;
    }
}
